package bookmanagementsystem.gui;

import bookmanagementsystem.dao.AdminDAO;
import bookmanagementsystem.model.Admin;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminRegistrationFrame extends JFrame {
    private JTextField userIDField;
    private JPasswordField passwordField;
    private JTextField genderField;
    private JTextField phoneNumberField;
    private JTextField emailField;
    private JButton registerButton;
    private JButton clearButton;

    public AdminRegistrationFrame() {
        setTitle("图书管理系统 - 管理员注册");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents();
        addComponentsToFrame();
        addActionListeners();
    }

    private void initComponents() {
        userIDField = new JTextField(20);
        passwordField = new JPasswordField(20);
        genderField = new JTextField(20);
        phoneNumberField = new JTextField(20);
        emailField = new JTextField(20);
        registerButton = new JButton("注册");
        clearButton = new JButton("清除");
    }

    private void addComponentsToFrame() {
        JPanel panel = new JPanel(new GridLayout(6, 2, 10, 10));

        // 对标签设置水平对齐方式为右对齐
        JLabel userIDLabel = new JLabel("管理员ID：");
        userIDLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(userIDLabel);
        panel.add(userIDField);

        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(passwordLabel);
        panel.add(passwordField);







        panel.add(registerButton);
        panel.add(clearButton);

        getContentPane().add(panel);
    }

    private void addActionListeners() {
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userID = userIDField.getText();
                String password = new String(passwordField.getPassword());
                String gender = genderField.getText();
                String phoneNumber = phoneNumberField.getText();
                String email = emailField.getText();

                Admin user = new Admin(userID, password);
                AdminDAO userDAO = new AdminDAO();
                if (userDAO.registerAdmin(user)) {
                    JOptionPane.showMessageDialog(AdminRegistrationFrame.this, "注册成功！");
                    // 注册成功后可以选择关闭注册窗口
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(AdminRegistrationFrame.this, "注册失败，请检查输入信息是否正确。");
                }
            }
        });

        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userIDField.setText("");
                passwordField.setText("");
                genderField.setText("");
                phoneNumberField.setText("");
                emailField.setText("");
            }
        });
    }

    public static void main(String[] args) {

        AdminRegistrationFrame AdminRegistrationloginFrame = new AdminRegistrationFrame();
    }
}